<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 店铺信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-form-item label="名称" prop="name" >
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="图标" >
                    <el-upload
                            ref="upload"
                            class="avatar-uploader"
                            action=""
                            :http-request="fileUpload"
                            :show-file-list="false"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="form.image" :src="form.image" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="电话" >
                    <el-input v-model="form.tel"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </div>

    </div>
</template>

<script>
import { shopInfo,shopUpdate } from '../../../api/shop/shop';

import { fileUpload } from '../../../api/uploadFile';

export default {
    name: 'shop-banner-list',
    data() {
        return {
            editVisible:false,
            form:{
                name:"",
                image:"",
                tel:"1"
            },
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ]
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            shopInfo({}).then(res => {
                this.form = res.shop;
            });
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                let data = {id:this.form.id,name:this.form.name,image:this.form.image,tel:this.form.tel}
                shopUpdate(data).then(res => {
                    if (res.code == 0) {
                        this.$message.success(`修改成功`);
                        this.getData();
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                });
            });
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        fileUpload(file){
            let _this = this;
            return fileUpload(file).then(res => {
                // 多图片上传，结果data里面是一个array
                // console.log(res.data[0]);
                _this.form.image = res.data[0]
                // _this.handleAvatarSuccess(res, file)
                _this.$refs.upload.clearFiles();//清空
            });
        }
    }
};
</script>

<style scoped>
.avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    width: 148px;
    height: 148px;
    position: relative;
    overflow: hidden;
}
.avatar-uploader:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
}
.avatar {
    width: 148px;
    height: 148px;
    display: block;
}
</style>
